package com.yy.ehcache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EhcacheStore {
	private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheStore.class);
	public final Ehcache myehcache = new Ehcache();
	
	public void init() throws Exception {
		myehcache.setupCache();
	}
	
	
	public synchronized void addPeep() throws Exception {
		LOGGER.info("从缓存清除");
		myehcache.clearCache();
	}
	
	public synchronized String findAllPeeps(String sky,String result) throws Exception {
		// find from cache 1st
		String fromCache = myehcache.getFromCache("logbig");
		if (fromCache != null) {
			LOGGER.info("从缓存读取数据");
			return fromCache;
		}

		LOGGER.info("填充缓存");
		myehcache.addToCache(sky,result);
		return result;
	}
	
	public synchronized String findVal(String sky) {
		if(this.findBoolean(sky)){
			String fromCache = myehcache.getFromCache(sky);
			return fromCache;
		}
		return null;
	}
	public synchronized String addVal(String sky,String val) {
		LOGGER.info("填充缓存");
		myehcache.addToCache(sky,val);
		return val;
	}
	
	public synchronized Boolean findBoolean(String sky) {
		// find from cache 1st
		String fromCache = myehcache.getFromCache(sky);
		if (fromCache != null) {
			LOGGER.info("存在数据");
			return true;
		}
		return false;
	}
	
	public void close() throws Exception {
		myehcache.close();
	}
}
